package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;


    @Override
    public List<Integer> findRoleById(int id) {
        List<Integer> roleIds = roleDao.findRoleById(id);
        return roleIds;
    }

    @Override
    public List<Integer> findRoleByMenuId(String menuId) {
        List<Integer> roleId = roleDao.findRoleByMenuId(Integer.parseInt(menuId));
        return roleId;
    }

    @Override
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //取出封装数据
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增检查组，同时需要关联检查项（设置多对多关系）
     *
     * @param role
     * @param permissionIds
     */

    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //新增检查组
        roleDao.add(role);
        Integer roleId = role.getId();//检查组id

        //设置角色和权限多对多关系
        this.setRoleAndPermission(roleId, permissionIds);

        //设置角色和菜单多对多关系
        this.setRoleAndMenu(roleId, menuIds);

    }

    /**
     * 查询数据
     *
     * @return
     */
    public List<Role> findAll() {
        return roleDao.findAll();
    }


    /**
     * 删除
     *
     * @param id
     */

    @Override
    public void delete(Integer id) {
        //根据检查组id删除中间表数据（清理原有关联关系）
        roleDao.deleteAssociation(id);

        roleDao.deleteAssociationMenuIds(id);
        //根据id删除检查组记录
        roleDao.deleteById(id);

    }

    @Override
    public Role findById(int id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionByRoleId(int id) {
        return roleDao.findPermissionByRoleId(id);
    }

    @Override
    public List<Integer> findMenuByRoleId(int id) {
        return roleDao.findMenuByRoleId(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        Integer roleId = role.getId();
        //根据检查组id删除中间表数据（清理原有关联关系）
        roleDao.deleteAssociation(roleId);
        roleDao.deleteAssociationMenuIds(roleId);

        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        setRoleAndPermission(roleId, permissionIds);
        setRoleAndMenu(roleId, menuIds);
        //更新检查组基本信息
        roleDao.edit(role);

    }

    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        //设置多对多关系
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {//检查项id
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    public void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        //设置多对多关系
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {//检查项id
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
}
